﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 16/11/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;


namespace ARSS.Host.Visual
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Variables

        /// <summary>
        /// Selected client's name
        /// </summary>
        private string selectedClientName;

        #endregion

        #region Functions
        
        /// <summary>
        /// Constructor
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        /// <summary>
        /// MainWindow loaded routed event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Setup the ClientsItemsControl binding
            Binding b = new Binding();
            b.Source = Core.Instance.ClientList;
            b.Mode = BindingMode.OneWay;
            ClientsItemsControl.SetBinding(ItemsControl.ItemsSourceProperty, b);

            Core.Instance.ActiveWindowObject = this;

            Core.Instance.IsSessionStarted = false;

        }

        /// <summary>
        /// Client item canvas mouse enter event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Canvas_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // Show the highlight border
            ((sender as Canvas).FindName("ItemBorder") as Border).Visibility = Visibility.Visible;

            // Set selected client
            this.selectedClientName = ((sender as Canvas).FindName("NameLabel") as Label).Content.ToString();
        }

        /// <summary>
        /// Client item canvas mouse leave event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Canvas_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // Hide the highlight border
            ((sender as Canvas).FindName("ItemBorder") as Border).Visibility = Visibility.Hidden;

            Button btn = (sender as Canvas).FindName("KickButton") as Button;
            btn.Visibility = Visibility.Hidden;

            // Release selected client
            this.selectedClientName = "";
        }

        /// <summary>
        /// Client item canvas mouse right button up event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Canvas_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Button btn = (sender as Canvas).FindName("KickButton") as Button;
            if (btn.Visibility == Visibility.Hidden)
                btn.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Client item kick button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void KickButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// Kick selected client
            foreach (ClientData client in Core.Instance.ClientList)
            {
                if (client.Name.CompareTo(this.selectedClientName) == 0)
                {
                    client.Disconnect();
                    break;
                }
            }

            this.selectedClientName = "";
        }

        /// <summary>
        /// Open file button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OpenFileButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "XML documents |*.xml";

            Nullable<bool> result = dialog.ShowDialog();

            if (result == true)
            {
                SceneFileTextBox.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// Start button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void HostButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (HostButton.Content.ToString().CompareTo("Host") == 0) // To host?
            {
                
                if (SceneFileTextBox.Text.Length == 0 || PortTextBox.Text.Length == 0)
                {
                    StatusTextBlock.Text = "Please fill in the Text Boxes on top.";
                    return;
                }

                StatusTextBlock.Text = "Hosting on port " + PortTextBox.Text + "...";
                HostButton.Content = "Cancel";
                OpenFileButton.IsEnabled = false;
                SceneFileTextBox.IsEnabled = false;
                PortTextBox.IsEnabled = false;

                try
                {
                    Core.Instance.SceneFilePath = SceneFileTextBox.Text;
                    Core.Instance.StartHosting(int.Parse(PortTextBox.Text));

                    StartButton.IsEnabled = true;

                    Logger.Instance.Log("Hosted on port " + PortTextBox.Text);
                }
                catch (Exception ex)
                {
                    StatusTextBlock.Text = "Failed to host";
                    Logger.Instance.Log("Could not host, reason: " + ex.Message);
                }
            }
            else
            {
                // To cancel

                StatusTextBlock.Text = "";
                HostButton.Content = "Host";
                OpenFileButton.IsEnabled = true;
                SceneFileTextBox.IsEnabled = true;
                PortTextBox.IsEnabled = true;

                Core.Instance.StopHosting();

                Logger.Instance.Log("Stopped hosting.");

                StartButton.IsEnabled = false;
            }
        }

        protected void StartButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                Core.Instance.StartSession();

                // Hide start button
                StartButton.IsEnabled = false;

                Logger.Instance.Log("Session started");
            }
            catch (Exception ex)
            {
                Logger.Instance.Log("Could not start session, reason: " + ex.Message);
            }
        }

        #endregion
    }
}
